package cn.iocoder.yudao.module.trade.controller.admin.order;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.pickup.DeliveryPickUpStoreRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.AdminTradeOrderCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.MemberUserAddressRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderCreateRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.stationinfo.vo.StationInfoRespVO;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderCreateRespVO;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderDetailRespVO;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryPickUpStoreService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderCreateService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import cn.iocoder.yudao.module.trade.service.stationinfo.StationInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 下单")
@RestController
@RequestMapping("/trade/order/place")
@Validated
@Slf4j
public class TradeOrderOpController {

    @Resource
    private TradeOrderQueryService tradeOrderQueryService;
    @Resource
    private DeliveryPickUpStoreService deliveryPickUpStoreService;
    @Resource
    private StationInfoService stationInfoService;
    @Resource
    private TradeOrderCreateService tradeOrderCreateService;
    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;

    @GetMapping("/crm/member/info")
    @PermitAll
    @Operation(summary = "获得燃气户信息")
    public CommonResult<MemberUserAddressRespVO> getCrmMemberInfo(TradeOrderCreateRespVO tradeOrderCreateRespVO){
        MemberUserAddressRespVO memberUserRespVO = tradeOrderQueryService.getCrmMemberByVO(tradeOrderCreateRespVO);
        return success(memberUserRespVO);
    }


    @GetMapping("/crm/his/list")
    @PermitAll
    @Operation(summary = "获得历史订单")
    @Parameter(name = "userId", description = "用户编号", required = true)
    public CommonResult<PageResult<AppTradeOrderDetailRespVO>> getHisOrderPage(Long userId){
        AppTradeOrderPageReqVO appTradeOrderPageReqVO = new AppTradeOrderPageReqVO();
        return success(BeanUtils.toBean(tradeOrderQueryService.getOrderPage(userId,appTradeOrderPageReqVO),AppTradeOrderDetailRespVO.class));
    }

    @GetMapping("/store/info")
    @PermitAll
    @Operation(summary = "获取门店列表")
    public CommonResult<List<DeliveryPickUpStoreRespVO>> getUpStoreList(){
        return success(BeanUtils.toBean(deliveryPickUpStoreService.getDeliveryPickUpStoreList(),DeliveryPickUpStoreRespVO.class));
    }
    @GetMapping("/station/info")
    @PermitAll
    @Operation(summary = "获取气站列表")
    public CommonResult<List<StationInfoRespVO>> getStationList(){
        return success(BeanUtils.toBean(stationInfoService.getStationAllList(),StationInfoRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    //@PreAuthenticated
    @PermitAll
    public CommonResult<AppTradeOrderCreateRespVO> createOrder(@Valid @RequestBody AdminTradeOrderCreateReqVO createReqVO,
                                                               @RequestHeader Integer terminal) {
        TradeOrderDO order = tradeOrderCreateService.createOrder(createReqVO.getUserId(),createReqVO.getDeliveryUserId(), getClientIP(), createReqVO, terminal);
        return success(new AppTradeOrderCreateRespVO().setId(order.getId()).setPayOrderId(order.getPayOrderId()));
    }

    @PutMapping("/receive")
    @Operation(summary = "确认交易订单收货")
    @Parameter(name = "id", description = "交易订单编号")
    public CommonResult<Boolean> receiveOrder(@RequestParam("id") Long id) {
        tradeOrderUpdateService.receiveOrderByMember(getLoginUserId(), id);
        return success(true);
    }
}
